package com.nf.action;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class StudentDaoImpl implements StudentDao{
    @Override
    public Student findStudentById(int stuId) {
        Connection conn;
        String jdbcUrl ="jdbc:mysql://localhost:3306/StudentDB?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true";
        Student student = null;
        try{
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection(jdbcUrl,"root","123456");
            PreparedStatement pst = conn.prepareStatement("select * from Student where id=?");
            pst.setInt(1,stuId);
            ResultSet rs = pst.executeQuery();
            if (rs.next()){
                student =new Student();
                student.setId( rs.getInt("id") );
                student.setName( rs.getString("name") );
                student.setAge( rs.getInt( "age"));
            }
            rs.close();
            pst.close();
            conn.close();
        }catch (Exception e){
            return null;
        }
        return student;
    }
}
